package com.jinzhi.jzweb.controller;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import com.jinzhi.common.constants.ConstMessages;
import com.jinzhi.common.constants.MessagesEnum;
import com.jinzhi.common.utils.ShiroUtils;
import com.jinzhi.common.utils.StringUtil;
import com.jinzhi.jzapi.domain.UserDO;
import com.jinzhi.jzweb.domain.JazMessageDO;
import com.jinzhi.jzweb.domain.JazSysContractDO;
import com.jinzhi.jzweb.domain.JazSysProjectDO;
import com.jinzhi.jzweb.domain.vo.JazUserOrderExcelVO;
import com.jinzhi.jzweb.domain.vo.JazUserOrderQueryVo;
import com.jinzhi.jzweb.domain.vo.JazUserOrderVO;
import com.jinzhi.jzweb.domain.vo.userAunt.UserVO;
import com.jinzhi.jzweb.service.*;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import com.jinzhi.sys.service.UserService;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.JazUserOrderDO;
import com.jinzhi.common.utils.Result;

import javax.servlet.http.HttpServletResponse;

/**
 * 
 * <pre>
 * 订单管理
 * </pre>
 * <small> 2023-04-18 09:38:41 | Long</small>
 */
@Controller
@RequestMapping("/jzweb/jazUserOrder")
public class JazUserOrderController extends BaseController {
	@Autowired
	private JazUserOrderService jazUserOrderService;
	@Autowired
	private UserService userService;
	@Autowired
	private JazSysContractService jazSysContractService;
	@Autowired
	private JazSysProjectService jazSysProjectService; //服务表

	@Autowired
	private JazMessageService jazMessageService;

	/**
	 * 月嫂信息
	 */
	@Autowired
	JazUserAuntService jazUserAuntService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:jazUserOrder:jazUserOrder")
	String JazUserOrder(Model model){
		model.addAttribute("type", "订单");
		return "jzweb/jazGroupActivity/jazGroupActivity";
//	    return "jzweb/jazUserOrder/jazUserOrder";
	}


	@GetMapping("/info")
	String info(Model model){
		return "jzweb/jazUserOrder/info";
	}

	/**
	 * 查询服务订单
	 * @return
	 */
	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("jzweb:serviceList:serviceList")
	public Result<?> list(@RequestBody JazUserOrderQueryVo jazUserOrderQueryVo){
		// 查询列表数据
		Page<JazUserOrderVO> page = jazUserOrderService.selectOrderList(jazUserOrderQueryVo);
        return Result.ok(page);
	}

	/**
	 * 查看详情
	 * @param id
	 * @return
	 */
	@GetMapping("/getInfo")
	@ResponseBody
	public Result<JazUserOrderVO> getInfo(@RequestParam("id") Long id){
		JazUserOrderVO jazUserOrderVO = jazUserOrderService.selectOrderDetail(id);
		return Result.ok(jazUserOrderVO);
	}



	@GetMapping("/add")
	@RequiresPermissions("jzweb:jazUserOrder:add")
	String add(){
	    return "jzweb/jazUserOrder/add";
	}


	/**
	 * 更改订单详情备注
	 * */
	@PostMapping("/edit")
	@ResponseBody
	public Result<String> edit(JazUserOrderDO  jazUserOrderDO){
		EntityWrapper<JazUserOrderDO>  Wrapper = new EntityWrapper<>();
		Wrapper.eq("id",jazUserOrderDO.getId());
		jazUserOrderService.update(jazUserOrderDO,Wrapper);
//		JazUserOrderDO jazUserOrder = jazUserOrderService.selectById(id);
//		model.addAttribute("jazUserOrder", jazUserOrder);
//	    return "jzweb/jazUserOrder/edit";
		return Result.ok();
	}

	@ResponseBody
	@PostMapping("/save")
	public Result<String> save( JazUserOrderDO jazUserOrder){
		jazUserOrderService.insert(jazUserOrder);
        return Result.ok();
	}
	
	@Log(value = "服务列表-保洁-修改",sysModule = 5,operationType = 3)
	@ResponseBody
	@PostMapping("/update")
	@Transactional(rollbackFor = Exception.class)
	public Result<String>  update(@RequestBody JazUserOrderDO jazUserOrder){
		JazUserOrderDO jazUserOrderDO = jazUserOrderService.selectById(jazUserOrder.getId());
		if (jazUserOrderDO == null){
			return Result.fail();
		}
		jazUserOrderDO.setSigningId(jazUserOrder.getSigningId());
		UserDO userDO = userService.selectById(jazUserOrder.getSigningId());
		if (userDO == null){
			return Result.fail();
		}
		jazUserOrderDO.setNameYi(userDO.getName());
		jazUserOrderDO.setPhoneYi(userDO.getOrderPhone());
		StringBuffer buffer = new StringBuffer();
		if (StringUtil.notEmpty(userDO.getProvince())){
			buffer.append(userDO.getProvince());
		}
		if (StringUtil.notEmpty(userDO.getCity())){
			buffer.append(userDO.getCity());
		}
		if (StringUtil.notEmpty(userDO.getDistrict())){
			buffer.append(userDO.getDistrict());
		}
		if (StringUtil.notEmpty(userDO.getDetailedAddress())){
			buffer.append(userDO.getDetailedAddress());
		}
		jazUserOrderDO.setHomeAddressYi(buffer.toString());

		boolean update = jazUserOrderService.updateById(jazUserOrderDO);
		// 发送消息  阿姨
		JazMessageDO jazMessageDO1 = new JazMessageDO();
		jazMessageDO1.setMessageType(1); //系统消息
		jazMessageDO1.setSenderId(ShiroUtils.getUserId()); //发送人
		jazMessageDO1.setReceiverId(jazUserOrderDO.getSigningId()); //接收人
		jazMessageDO1.setMessageContent(MessagesEnum.MESSAGE_208.getMsg());
		jazMessageDO1.setMessageNum(MessagesEnum.MESSAGE_208.getNum());
		jazMessageDO1.setHasSendSms(true);
		jazMessageDO1.setUserOrderId(jazUserOrderDO.getId());
//		jazMessageDO1.setObjectId(jazUserOrderDO.getId());
		jazMessageDO1.setIsRead(0);
		jazMessageService.senMessage(jazMessageDO1);
		// 用户
		JazMessageDO jazMessageDO2 = new JazMessageDO();
		jazMessageDO2.setMessageType(1); //系统消息
		jazMessageDO2.setSenderId(ShiroUtils.getUserId()); //发送人
		jazMessageDO2.setReceiverId(jazUserOrderDO.getInitiateId()); //接收人
		jazMessageDO2.setMessageContent(MessagesEnum.MESSAGE_207.getMsg());
		jazMessageDO2.setMessageNum(MessagesEnum.MESSAGE_207.getNum());
//		jazMessageDO2.setObjectId(jazUserOrderDO.getId());
		jazMessageDO2.setIsRead(0);
		jazMessageService.senMessage(jazMessageDO2);
		return update ? Result.ok() : Result.fail();
	}

	@Log(value = "服务列表-月嫂-修改",sysModule = 5,operationType = 3)
	@ResponseBody
	@PostMapping("/updateUserOrderAunt")
	public Result<String>  updateUserOrderAunt(@RequestBody JazUserOrderVO jazUserOrderVO){
		return jazUserOrderService.updateUserOrderAunt(jazUserOrderVO);
	}

	@Log(value = "服务列表-修改审核状态",sysModule = 5,operationType = 3)
	@ResponseBody
	@PostMapping("/updateAuditResult")
	public Result<String>  updateAuditResult(@RequestBody JazUserOrderDO jazUserOrder){
		return jazUserOrderService.updateAuditResult(jazUserOrder);
	}

	@Log(value = "服务列表-服务转让",sysModule = 5,operationType = 3)
	@ResponseBody
	@PostMapping("/serviceTransfer")
	public Result serviceTransfer(@RequestBody JazUserOrderVO jazUserOrder){
		return jazUserOrderService.serviceTransfer(jazUserOrder);
	}

	/**
	 * 服务转让根据id查询
	 * @param id
	 * @return
	 */
	@ResponseBody
	@GetMapping("/serviceTransferById")
	public Result serviceTransferById(@RequestParam("id") Long id){
		return jazUserOrderService.serviceTransferById(id);
	}
	
	@Log(value = "删除",sysModule = 5,operationType = 2)
	@PostMapping( "/remove")
	@ResponseBody
	public Result<String>  remove( Long id){
		jazUserOrderService.deleteById(id);
        return Result.ok();
	}


	@ApiOperation(value = "查服务信息", notes = "查保洁信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "", value = "", required = true, dataType = ""),
	})
	@ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
			@ApiResponse(code = 500, message = "服务器内部异常")})
	@ResponseBody
	@GetMapping(value = "/getUserAunt")
	public Result<List<UserVO>> getUserAunt(@RequestParam("id") String id,@RequestParam("projectId") String projectId){
		return Result.ok(jazUserOrderService.getUserAunt(id,projectId));
	}

	@ApiOperation(value = "查保洁信息", notes = "查保洁信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "", value = "", required = true, dataType = ""),
	})
	@ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
			@ApiResponse(code = 500, message = "服务器内部异常")})
	@ResponseBody
	@GetMapping(value = "/getUserAuntBaoJie")
	public Result<List<UserVO>> getUserAuntBaoJie(){
		return Result.ok(jazUserOrderService.getUserAuntBaoJie());
	}


	/**
	 * 订单信息导出
	 *
	 * @param response
	 */
	@GetMapping("/downloadImportStu")
	@ResponseBody
	public void downloadImportTemplate(HttpServletResponse response, JazUserOrderVO jazUserOrderVO) {
		List<JazUserOrderExcelVO> list1 = new LinkedList<>();
		List<JazUserOrderVO> list=	jazUserOrderService.JazUserOrderExcel(jazUserOrderVO);
		for(JazUserOrderVO li:list){
			JazUserOrderExcelVO jazUserOrderVO1 = new JazUserOrderExcelVO();
			jazUserOrderVO1.setId(li.getId());//订单编号
			if (li.getNickname() != null && li.getNickname() != ""){
				jazUserOrderVO1.setName(li.getNickname());
			}else {
				jazUserOrderVO1.setName(li.getMobile());
			}
			jazUserOrderVO1.setMobile(li.getMobile());
			if (li.getCreateTime() != null){
				DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
				String  dateStr = sdf.format(li.getCreateTime());
				jazUserOrderVO1.setCreateTime(dateStr);
			}
			if (li.getStatus() == 0){
				jazUserOrderVO1.setStatus("待开始");
			}else if (li.getStatus() == 1){
				jazUserOrderVO1.setStatus("进行中");
			}else if (li.getStatus() == 2){
				jazUserOrderVO1.setStatus("已完成");
			}else if (li.getStatus() == 3){
				jazUserOrderVO1.setStatus("已取消");
			}
			jazUserOrderVO1.setRemark(li.getRemark());
			list1.add(jazUserOrderVO1);
		}
		//导出操作
		EasyPoiUtil.exportExcel(list1, "订单信息导出", "订单信息导出", JazUserOrderExcelVO.class, "订单信息导出", response);
	}



}
